var flag = 0;
var table;
getData();
var faults = undefined;

function getData(tid) {
	var tid = $('#tid').val();
	var isForecast = $('#isForecast').val() ? 1 : 0;
	var equipmentId = $('#equipmentId').val();
	var voltage = $('#voltage').val();
	if (tid) {
		$.getJSON('/rest/TyphoonRestService/getFaultByTyphoonId/' + tid + '/' + voltage, function(result) {
			faults = result;
			showTyphoonFault(faults[0].historyTyphoon.tname);
		});
	} else if (equipmentId) {
		$.getJSON('/rest/TyphoonRestService/getTyphoonFaultsByEquipmentId/' + equipmentId, function(result) {
			faults = result;
			showEquipmentFault();
		});
	}
}
function showTyphoonFault(typhoonNames) {
	$('#typhoonTitle').html('<strong>' + typhoonNames + " </strong><br/>共发生相关台风故障  " + faults.length + " 次");
	drawTable();
}
function showEquipmentFault() {
	$('#typhoonTitle').html('<strong>' + faults[0].faultRecord.equipment + " </strong><br/>台风期间发生的故障" + faults.length + "次");
	drawTable();
}
function drawTable() {
	var opt = MyTable.getDefaultOptions();
	opt.paging = false;
	opt.scrollY = '500px';
	opt.columnDefs = [ {
		"width" : "12%",
		"targets" : [ 2, 3, 5, 6 ]
	}, {
		"width" : "5%",
		"targets" : [ 0 ]
	}, {
		"width" : "8%",
		"targets" : [ 1, 7, 8 ]
	}, {
		"width" : "23%",
		"targets" : 4
	} ];
	opt.autoWidth = false;
	var columns = [ '', '调管范围', '故障时间', '故障设备', '故障过程', '行波测距', '备注', '风圈', '详细' ];
	table = new MyTable('faultTable', columns, opt);
	table.setDataSet(getDataset());
	table.show();
}
function getDataset() {
	var dataset = [];
	for ( var i in faults) {
		var f = faults[i].faultRecord;
		var lineid = f['equipment_id'];
		var ftime = time(f.faultTime);
		var faultReport = '<button class="btn btn-sm btn-primary fa fa-search" onclick="showFaultReport(\'' + lineid
				+ '\',\'' + ftime + '\')"></button>';
		dataset.push([ Number(i) + 1, str(f.rank), ftime, str(f.equipment), str(f.description), str(f.ranging),
				str(f.remark), getRadius(faults[i].radius), faultReport ]);
	}
	return dataset;
}
function getRadius(radius){
	var result=radius;
	if(radius=="0"){
		result="小于7级";
	}else if(radius=="-1"){
		result="无地理信息";
	}
	return result;
}
function showFaultReport(lineid, faultTime) {
	var fra = document.createElement("iframe");
	fra.style = "background:transparent;border:0;";
	fra.width = "100%";
	fra.id = "faultReportIframe";
	fra.height = "650px";
	var s = "/ui/map/protectActSvg/protectActSvg.xhtml?lineId=" + lineid + "&faultTime=" + faultTime
			+ "&showBackButton=" + "true";
	fra.src = s;
	document.body.appendChild(fra);
	$('#mainPage').css('display', 'none');
};
function backFromFaultReport() {
	document.body.removeChild(document.getElementById("faultReportIframe"));
	$('#mainPage').css('display', 'block');
	table.getTable().draw();
}
function getCutString(str) {
	return str ? (str.length > 20 ? '<span title="' + str + '">' + str.substring(0, 20) + '...</span>' : str) : '';
}
function str(v) {
	return v ? v : '';
}
function time(v) {
	if (!v) {
		return '';
	} else {
		var t = new Date(v.time ? v.time : v);
		var m = t.getMonth() + 1 > 9 ? (t.getMonth() + 1) : ("0" + (t.getMonth() + 1));
		var d = t.getDate() > 9 ? t.getDate() : "0" + t.getDate();
		var h = t.getHours() > 9 ? t.getHours() : "0" + t.getHours();
		var min = t.getMinutes() > 9 ? t.getMinutes() : "0" + t.getMinutes();
		var s = t.getSeconds() > 9 ? t.getSeconds() : "0" + t.getSeconds();
		var timestr = t.getFullYear() + "-" + m + "-" + d + " " + h + ":" + min + ":" + s;
		return timestr;
	}
}

function resizeHtml() {
	if (table) {
		table.getTable().draw();
	}
}